package com.learn.project2.service.admin.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.*;
import com.learn.project2.config.customException.ShipException;
import com.learn.project2.mapper.*;
import com.learn.project2.model.bo.admin.order.OrderListBO;
import com.learn.project2.model.bo.admin.order.OrderRefundBO;
import com.learn.project2.model.bo.admin.order.OrderReplyBO;
import com.learn.project2.model.bo.admin.order.OrderShipBO;
import com.learn.project2.model.vo.admin.order.OrderChannelVO;
import com.learn.project2.model.vo.admin.order.OrderDetailVO;
import com.learn.project2.model.vo.admin.order.OrderListVO;
import com.learn.project2.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.jws.Oneway;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * \* Author: Valley
 * \* Date: 2022/1/6
 * \* Time: 22:08
 * \* Description:
 */

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    MarketOrderChannelMapper channelMapper;

    /**
     * @description: 订单管理-查询
     * @param: [orderListBO]
     * @return: com.learn.project2.model.vo.admin.order.OrderListVO
     * @author 王力
     * @date: 2022-01-07 15:53:14
     */
    @Override
    public OrderListVO orderList(OrderListBO orderListBO) {
        PageHelper.startPage(orderListBO.getPage(), orderListBO.getLimit());
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria().andDeletedEqualTo(false);
        if (orderListBO.getUserId() != null) criteria.andUserIdEqualTo(orderListBO.getUserId());
        if (orderListBO.getOrderId() != null && !"".equals(orderListBO.getOrderId())) criteria.andIdEqualTo(orderListBO.getOrderId());
        if (orderListBO.getOrderSn() != null) criteria.andOrderSnEqualTo(orderListBO.getOrderSn());
        if (orderListBO.getOrderStatusArray() != null)
            criteria.andOrderStatusEqualTo(orderListBO.getOrderStatusArray());
        if (orderListBO.getStart() != null) criteria.andAddTimeGreaterThan(orderListBO.getStart());
        if (orderListBO.getEnd() != null) criteria.andUpdateTimeLessThan(orderListBO.getEnd());
        List<MarketOrder> marketOrders = orderMapper.selectByExample(orderExample);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        long total = pageInfo.getTotal();
        OrderListVO orderListVO = new OrderListVO();
        orderListVO.setLimit(orderListBO.getLimit());
        orderListVO.setPage(orderListBO.getPage());
        orderListVO.setList(pageInfo.getList());
        orderListVO.setTotal(total);
        orderListVO.setPages((int) Math.ceil((double) total / orderListBO.getLimit()));
        return orderListVO;
    }


    /**
     * @description: 订单管理-详情
     * @param: [id]
     * @return: com.learn.project2.model.vo.admin.order.OrderDetailVO2
     * @author 王力
     * @date: 2022-01-07 16:56:54
     */
    @Override
    public OrderDetailVO orderDetail(Integer id) {
        MarketOrder order = orderMapper.selectByPrimaryKey(id);
        MarketOrderGoodsExample goodsExample = new MarketOrderGoodsExample();
        goodsExample.createCriteria().andOrderIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(goodsExample);
        MarketUser marketUser = userMapper.selectByPrimaryKey(order.getUserId());
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setOrder(order);
        orderDetailVO.setOrderGoods(marketOrderGoods);
        orderDetailVO.setUser(marketUser);
        return orderDetailVO;
    }

    /**
     * @description: 订单管理-删除
     * @param: [orderShipBO]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-07 17:26:54
     */
    @Override
    public Integer orderShip(OrderShipBO orderShipBO) throws ShipException {
        if (orderShipBO == null || orderShipBO.getShipChannel() == null
                || orderShipBO.getShipSn() == null) throw new ShipException();
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrder order = orderMapper.selectByPrimaryKey(orderShipBO.getOrderId());
        order.setOrderStatus((short)301);
        order.setShipChannel(orderShipBO.getShipChannel());
        order.setShipSn(orderShipBO.getShipSn());
        order.setShipTime(StringUtils.dateToDate(new Date()));
        int i = orderMapper.updateByPrimaryKey(order);
        return i;
    }


    /**
     * @description: 订单管理-删除
     * @param: [orderId]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-07 17:30:20
     */
    /** 
     * @description:
     * @param: 修改为逻辑删除，并查询这条记录返回给orderController
     * @return:
     * @author zhizhuke
     * @date: 2022-01-09 16:12
     */
    @Transactional
    @Override
    public Integer orderDelete(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setDeleted(true);
        int i = orderMapper.updateByPrimaryKeySelective(marketOrder);
        if (i==1){
            return 200;
        }
        return 404;
    }

    @Override
    public List<MarketOrderChannel> orderChannel() {
        MarketOrderChannelExample channelExample = new MarketOrderChannelExample();
        channelExample.createCriteria();
        List<MarketOrderChannel> channels = channelMapper.selectByExample(channelExample);
        return channels;
    }

    /**
     * @description: 商品管理-商品评论-回复
     * @param: [replyBO]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-08 14:25:10
     */
    @Override
    public Integer orderReply(OrderReplyBO replyBO) {
        MarketComment comment = commentMapper.selectByPrimaryKey(replyBO.getCommentId());
        comment.setAdminContent(replyBO.getContent());
        comment.setUpdateTime(StringUtils.dateToDate(new Date()));
        commentMapper.updateByPrimaryKey(comment);
        return null;
    }

    /**
     * @description: 订单退款
     * @param: [refundBO]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-11 16:38:17
     */
    @Override
    public Integer orderRefund(OrderRefundBO refundBO) {
        MarketOrder order = orderMapper.selectByPrimaryKey(refundBO.getOrderId());
        order.setRefundAmount(refundBO.getRefundMoney());
        order.setRefundTime(StringUtils.dateToDate(new Date()));
        Integer i = orderMapper.updateByPrimaryKeySelective(order);
        return i;
    }
}
